package com.jiajunsong.blog.controller;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jiajunsong.blog.domain.Blog;
import com.jiajunsong.blog.domain.Comment;
import com.jiajunsong.blog.domain.Result;
import com.jiajunsong.blog.domain.User;
import com.jiajunsong.blog.repository.BlogRepository;
import com.jiajunsong.blog.repository.CommentRepository;
import com.jiajunsong.blog.repository.UserRepository;

@RestController
public class CommentController {
	@Autowired
	private CommentRepository commentRepository;
	@Autowired
	private BlogRepository blogRepository;

	@DeleteMapping("/comment")
	public Result delete(@RequestParam(required = true) Long commentId, @RequestParam(required = true) Long blogId,
			Authentication authentication) {
		if(authentication==null) {
			return new Result("未登录");
		}
		User user = (User) authentication.getPrincipal();
		Optional<Blog> optionalBlog = blogRepository.findById(blogId);
		if (optionalBlog.isPresent()) {
			Blog blog = optionalBlog.get();
			Boolean isCommentIdInBlog = blog.removeComment(commentId);
			if (isCommentIdInBlog) {
				Optional<Comment> optionComment = commentRepository.findById(commentId);
				if (optionComment.isPresent()) {
					Comment comment = optionComment.get();	
					if (comment.getUser().getId().longValue() == user.getId().longValue()) {
						blogRepository.save(blog);
						//commentRepository.deleteById(commentId);
						return new Result("",blog);
					}
					else {
						return new Result("你只能删除自己发布的评论");
					}
				} else {
					return new Result("跟俊指定的commentId查不到对应的comment");
				}

			}else {
				return new Result("要删除的评论并不属于此Blog，不能直接删除此条评论，否则这条评论所对应Blog的对应数据不会删除(blog_comments表里对应的数据不会被删除，但是其所对应的comment已经被删除");
			}
		} else {
			return new Result("根据指定的blogId没有找到对应的Blog");
		}
	}

	@PostMapping("/comment")
	public Result post(@RequestParam String content, @RequestParam Long blogId, Authentication authentication) {
		if (authentication == null) {
			new Result("未登录");
		}
		Optional<Blog> optionalBlog = blogRepository.findById(blogId);
		if(optionalBlog.isPresent()) {
			Blog blog = optionalBlog.get();
			User user = (User) authentication.getPrincipal();
			Comment comment = new Comment();
			comment.setContent(content);
			comment.setUser(user);
			//commentRepository.save(comment);//因为blog设置里级联保存，所以这个句可以不写，写了也没有问题。如果blog没有设置级联保存，不写这句就会出错
			blog.addComment(comment);
			blogRepository.save(blog);
			return new Result("",blog);
		}else {
			return new Result("根据此blogId找不到Blog");
		}
	}
	
	@GetMapping("/comment")
	public Result get(@RequestParam(required=true) Long id) {
		Optional<Comment> optionalComment = commentRepository.findById(id);
		if(optionalComment.isPresent()) {
			Comment comment = optionalComment.get();
			return new Result("",comment);
		}else {
			return new Result("Not Found");
		}
	}

}
